package fr.umlv.m2.jee.spij.ldap.connection;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;

/**
 * method to connect to LDAP server.
 *
 * @author lounes
 */
public final class LdapConnect {

    /**
     * Constructor final to avoid instance.
     */
    private LdapConnect() {
    }

    /**
     * Connects to LDAP server.
     *
     * @return DirContext the directory context.
     *
     * @throws NamingException exception.
     */
    public static DirContext connect() throws NamingException {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:10389/ou=system");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        return new InitialDirContext(env);
    }

}
